Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 3 | Stretch Coursework#1250
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 3 | Stretch Coursework#1250Richiealx wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
| return password.length < 5 ? false : true | ||
| } | ||
| // Password must be at least 5 characters long. | ||
| if (password.length < 5) { |
There was a problem hiding this comment.
Could password just not be provided at all?
There was a problem hiding this comment.
Hi @ykamal,
Good point, thank you.
I updated the function to handle the case where a password is not provided.
Previously the function accessed password.length, which would throw an error if the value was undefined.
I added an input validation check at the start of the function to ensure the password exists and is a string. If it is missing or not a string, the function now safely returns false.
I also added test cases to cover these scenarios.
| @@ -0,0 +1,33 @@ | |||
| // Validate whether a credit card number meets the rules from card-validator.md | |||
| function validateCreditCardNumber(cardNumber) { | |||
| // Rule 1: the value must be exactly 16 characters long and contain only digits. | |||
There was a problem hiding this comment.
writing down these rules as comments in code is very helpful for making sure you complete all of them and also helpful for debugging in plain english. good job
| @@ -0,0 +1,33 @@ | |||
| // Validate whether a credit card number meets the rules from card-validator.md | |||
| function validateCreditCardNumber(cardNumber) { | |||
There was a problem hiding this comment.
this is more of an advanced thing, but as a general rule if you're implementing a function that returns a boolean (true/false) its best to start the function name with "is" or "has" to make it clear to a reader that it returns a boolean. So this one could be called "isCreditCardNumberValid" for example
| let sum = 0; | ||
| for (const digit of cardNumber) { | ||
| sum += Number(digit); | ||
| } | ||
|
|
||
| if (sum <= 16) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
while this works, it is slightly inefficient as it requires you to loop through the entire list of digits before saying true/false even if the first 4 3 digits are more than 16 for example. Can you think of a way to make this more efficient and return earlier?
Learners, PR Template
Self checklist
Changelist
Completed the Sprint 3 stretch tasks.
Password Validator
password-validator.jspassword-validator.test.jsCredit Card Validator
card-validator.jscard-validator.test.jsTesting
All tests were run using:
All tests passed successfully.